package com.orhon.smartcampus.modules.student.fetcher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.HttpUtil;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("/statistics")
public class SearchsController {

	@Autowired
	private TokenService tokenService;
	
    public static Object search(String url, String query) {
    	String post = HttpUtil.sendPost("http://39.104.181.104:9200"+url,query);
    	Object parse = JSON.parse(post);
        return parse;
    }
    /**
     * 学校下 民族 数量 、届下学生数量
     * @Description
     *  @author bao
     *  @data Nov 12, 2019 5:27:57 PM
     */
	@GetMapping(value = "/nation")
	public Object nation() throws Exception {
		String token = tokenService.getSystemToken();
		String usertype = JwtUtils.getUserType(token);
		Long school_id = JwtUtils.getSchoolId(token);
		String query = "{\n" + 
				"  \"query\": {\n" + 
				"    \"bool\": {\n" + 
				"      \"must\": [\n" + 
				"        {\n" + 
				"          \"term\": {\n" + 
				"            \"school.id\": "+school_id+"" + 
				"          }\n" + 
				"        }\n" + 
				"      ]\n" + 
				"    }\n" + 
				"  },\n" + 
				"  \"from\": 0,\n" + 
				"  \"size\": 0,\n" + 
				"  \"aggs\": {\n" + 
				"    \"nation\": {\n" + 
				"      \"terms\": {\n" + 
				"        \"field\": \"student.nation.dictionary_value\"\n" + 
				"      }\n" + 
				"    },\n" + 
				"    \"student\": {\n" + 
				"      \"terms\": {\n" + 
				"        \"field\": \"arrive.id\"\n" + 
				"      }\n" + 
				"    }\n" + 
				"  }\n" + 
				"}";
		Object search = SearchsController.search("/smartcampus/learninfos/_search", query);
		return search;
	}

	/**
     * 学校下 性别 民族
     * @Description
     *  @author bao
     *  @data Nov 12, 2019 5:27:57 PM
     */
	@GetMapping(value = "/SchoolSex")
	public Object schoolSex() throws Exception {
		String token = tokenService.getSystemToken();
		String usertype = JwtUtils.getUserType(token);
		Long school_id = JwtUtils.getSchoolId(token);
		String query = "{\n" + 
				"  \"query\": {\n" + 
				"    \"bool\": {\n" + 
				"      \"must\": [\n" + 
				"        {\n" + 
				"          \"term\": {\n" + 
				"            \"school.id\": "+school_id+"" + 
				"          }\n" + 
				"        }\n" + 
				"      ]\n" + 
				"    }\n" + 
				"  },\n" + 
				"  \"from\": 0,\n" + 
				"  \"size\": 0,\n" + 
				"  \"aggs\": {\n" + 
				"    \"gender\": {\n" + 
				"      \"terms\": {\n" + 
				"        \"field\": \"student.gender.dictionary_value\"\n" + 
				"      }\n" + 
				"    },\n" + 
				"    \"nation\": {\n" + 
				"      \"terms\": {\n" + 
				"        \"field\": \"student.nation.dictionary_value\"\n" + 
				"      }\n" + 
				"    }\n" + 
				"   }\n" + 
				"  }\n" + 
				"}";
		Object search = SearchsController.search("/smartcampus/learninfos/_search", query);
		return search;
	}

	/**
     * 学校下 性别 民族
     * @Description
     *  @author bao
     *  @data Nov 12, 2019 5:27:57 PM
     */
	@GetMapping(value = "/SchoolArrives")
	public Object schoolArrive() throws Exception {
		String token = tokenService.getSystemToken();
		String usertype = JwtUtils.getUserType(token);
		Long school_id = JwtUtils.getSchoolId(token);
		String query = "{\n" + 
				"  \"query\": {\n" + 
				"    \"bool\": {\n" + 
				"      \"must\": [\n" + 
				"        {\n" + 
				"          \"term\": {\n" + 
				"            \"school.id\": "+school_id+"\n" + 
				"          }\n" + 
				"        }\n" + 
				"      ]\n" + 
				"    }\n" + 
				"  },\n" + 
				"  \"size\": 0,\n" + 
				"  \"aggs\": {\n" + 
				"    \"student\": {\n" + 
				"      \"terms\": {\n" + 
				"        \"field\": \"arrive.id\"\n" + 
				"      }\n" + 
				"    }\n" + 
				"  }\n" + 
				"}";
		Object search = SearchsController.search("/smartcampus/learninfos/_search", query);
		return search;
	}

}
